package com.hirsi.module.task.service;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.CharsetUtil;
import com.hirsi.core.thread.UploadThread;
import com.hirsi.core.util.ConfigUtil;
import com.hirsi.core.util.DbUtil;
import com.hirsi.core.util.RarUtil;
import com.hirsi.core.util.TorrentUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TaskService {
    public static void create(String path) {
        try {
            String tmpDir = System.getProperty("java.io.tmpdir");
            if (!tmpDir.endsWith(File.separator)) tmpDir += File.separator;
            String targetPath = tmpDir + "skyeysnow-netdisk" + path.substring(path.lastIndexOf(File.separator));
            //String targetPath = "/config/tmp" + path.substring(path.lastIndexOf(File.separator));
            PreparedStatement ps = DbUtil.connection.prepareStatement("insert into sn_task (path, target_path, create_time) values (?, ?, ?)");
            ps.setString(1, path);
            ps.setString(2, targetPath);
            ps.setString(3, LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));
            ps.execute();
            ResultSet gk = ps.getGeneratedKeys();
            if (gk.next()) {
                int id = gk.getInt(1);
                //创建压缩文件
                String logPath = RarUtil.compress(path, targetPath, id);
                ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS,
                        new LinkedBlockingQueue<>(1000), new ThreadPoolExecutor.CallerRunsPolicy());
                new Thread(() -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    //开始读取logPath
                    int i = 0;
                    while (RarUtil.taskId != null) {
                        try {
                            List<String> strings = FileUtil.readLines(logPath, CharsetUtil.CHARSET_UTF_8);
                            if (strings.size() != i) {
                                executor.execute(new UploadThread(id, strings.get(strings.size() - 1)));
                                i = strings.size();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                            Thread.sleep(1000 * 60);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                //生成torrent
                TorrentUtil.create(path);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


}
